package com.example.administrator.himalaya.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.adapter.RatingAdapter;
import com.example.administrator.himalaya.constants.HttpConstant;
import com.example.administrator.himalaya.entity.rating.TopBeans;
import com.example.administrator.himalaya.entity.rating.TopBeansAll;
import com.google.gson.Gson;
import com.rock.teachlibrary.ImageLoader;
import com.rock.teachlibrary.http.HttpUtil;

import java.util.List;


public class FragmentDicsoverRating extends BasicFragment implements HttpUtil.RequestCallBack, ExpandableListView.OnGroupClickListener {
    public static final String TAG = FragmentDicsoverRating.class.getName();
    private ExpandableListView expandableListView;
    private LayoutInflater inflater;
    private View headView;
    private RatingAdapter expandableAdapter;
    private ImageView headImageView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.fragment_discover_toplist, container, false);
        this.inflater = inflater;
        return layout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        expandableListView = ((ExpandableListView) layout.findViewById(R.id.top_list_expandable_list_view));
        expandableAdapter = new RatingAdapter(getActivity(), null);
        expandableListView.setAdapter(expandableAdapter);
        expandableListView.setOnGroupClickListener(this);
    }


    private void initData() {
        HttpUtil.getStringAsync(HttpConstant.TOPlIST_URL, this);
    }

    @Override
    public void onFailure() {

    }

    @Override
    public void onSuccess(String result, String url) {
        Gson gson = new Gson();
        TopBeansAll topBeansAll = gson.fromJson(result, TopBeansAll.class);
        String picUrl = topBeansAll.getFocusImages().getList().get(0).getPic();
        initHeadView();
        ImageLoader.display(headImageView, picUrl, R.mipmap.focus_img_nonet);
        List<TopBeans> datas = topBeansAll.getDatas();
        expandableAdapter.update(datas);

        int size = datas.size();
        for (int i = 0; i < size; i++) {
            expandableListView.expandGroup(i);
        }
    }

    private void initHeadView() {
        headView = inflater.inflate(R.layout.rating_head_image, null);
        headImageView = ((ImageView) headView.findViewById(R.id.top_list_focus_image));
        expandableListView.addHeaderView(headView);
    }

    /**
     * 返回值true 表示Group 不可以被点击
     *
     * @param parent
     * @param v
     * @param groupPosition
     * @param id
     * @return
     */
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        return true;
    }
}
